<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="课设第一天：写个简单的增删改。">
<meta name="keywords" content="学习笔记,SQL server">
<meta property="og:type" content="article">
<meta property="og:title" content="Sql server课设1">
<meta property="og:url" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;index.html">
<meta property="og:site_name" content="付文杰的博客">
<meta property="og:description" content="课设第一天：写个简单的增删改。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576326234446.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576326626561.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576326781383.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576328696247.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576329047524.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576329227325.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576329701221.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576329724257.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576330245223.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576330276141.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576330879544.png">
<meta property="og:updated_time" content="2019-12-15T12:08:29.791Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2019&#x2F;12&#x2F;14&#x2F;SQLServer%E8%AF%BE%E8%AE%BE1&#x2F;1576326234446.png">

<link rel="canonical" href="https://fwj1635387072.github.io/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>


  <title>Sql server课设1 | 付文杰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">付文杰的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">个人博客  |  日常</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>

      </div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://FWj1635387072.github.io/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/headImage.jpg">
      <meta itemprop="name" content="付文杰">
      <meta itemprop="description" content="兰州交通大学 | 本科生 <br> 信息管理与信息系统">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="付文杰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Sql server课设1
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-14 20:14:49" itemprop="dateCreated datePublished" datetime="2019-12-14T20:14:49+08:00">2019-12-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-12-15 20:08:29" itemprop="dateModified" datetime="2019-12-15T20:08:29+08:00">2019-12-15</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>课设第一天：写个简单的增删改。</p>
<a id="more"></a>

<p>ADO.NET是.NET框架的重要组件，用来完成C#连接数据库</p>
<h1 id="Connection连接对象"><a href="#Connection连接对象" class="headerlink" title="Connection连接对象"></a>Connection连接对象</h1><p>Connect对象也称数据库连接对象，Connection对象的功能是负责对数据源的连接。</p>
<h2 id="连接"><a href="#连接" class="headerlink" title="连接"></a>连接</h2><p>基本语法：数据源/服务器名称（Data Source）+ 数据库名称（Initial Catalog）+ 用户名（(User ID） + 密码（Password）</p>
<h3 id="Sql-server连接字符串"><a href="#Sql-server连接字符串" class="headerlink" title="Sql server连接字符串"></a>Sql server连接字符串</h3><p>标准安全连接：</p>
<p>Data Sorce=服务器名;Initial Catalog=数据库名;User Id=登陆名;Password=密码;</p>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> test1		<span class="comment">--使用该数据库</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> u1 <span class="keyword">from</span> login u1	<span class="comment">--创建用户</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">test</span>(a <span class="built_in">int</span> , b <span class="built_in">int</span>)<span class="comment">--创建表</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> <span class="keyword">test</span> <span class="keyword">to</span> u1			<span class="comment">--授权</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">test</span> <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>),(<span class="number">2</span>,<span class="number">2</span>)	<span class="comment">--插入数据</span></span><br></pre></td></tr></table></figure>

<h3 id="在VS中连接数据库"><a href="#在VS中连接数据库" class="headerlink" title="在VS中连接数据库"></a>在VS中连接数据库</h3><p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576326234446.png" alt="1576326234446"></p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576326626561.png" alt="1576326626561"></p>
<p>在VS中实现数据管理</p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576326781383.png" alt="1576326781383"></p>
<h3 id="连接到数据库"><a href="#连接到数据库" class="headerlink" title="连接到数据库"></a>连接到数据库</h3><p>Connection对象有两个重要属性：</p>
<p>（1）ConnectionString：表示用于打开 SQL Server 数据库的字符串； </p>
<p>（2）State：表示 Connection 的状态，有Closed和Open两种状态。</p>
<p>Connect对象的两个重要方法：</p>
<p>（1）Open()方法：指示打开数据库；</p>
<p>（2）Close()方法：指示关闭数据库。</p>
<h2 id="VS实例"><a href="#VS实例" class="headerlink" title="VS实例"></a>VS实例</h2><p>SqlCommand对象，也称数据库命令对象，SqlCommand对象主要执行curd操作的命令，也可以来执行存储过程。用于执行存储过程时需要将Command对象的CommandType 属性设置CommandType.StoredProcedure，默认情况下CommandType 属性为CommandType.Text，表示执行的是普通SQL语句。</p>
<p>Command主要有三个方法： </p>
<p>ExecuteNonQuery</p>
<p><strong>ExecuteNonQuery()：</strong>执行一个SQL语句，返回受影响的行数，这个方法主要用于执行对数据库执行增加、更新、删除操作，注意查询的时候不是调用这个方法。用于完成insert，delete，update操作。</p>
<p><strong>ExecuteScalar ()</strong>从数据库检索单个值。这个方法主要用于统计操作。ExecuteScalar ()这个方法是针对SQL语句执行的结果是一行一列的结果集，这个方法只返回查询结果集的第一行第一列。</p>
<p>executeScalar主要用于查询单行单列的值，如聚合函数（count,max,min,agv,sum）。</p>
<p><strong>ExecuteReader</strong>用于实现只进只读的高效数据查询。</p>
<p>ExecuteReader：返回一个SqlDataReader对象，可以通过这个对象来检查查询结果，它提供了只进只读的执行方式，即从结果中读取一行之后，移动到另一行，则前一行就无法再用。有一点要注意的是执行之后，要等到手动去调用Read()方法之后，DataReader对象才会移动到结果集的第一行，同时此方法也返回一个Bool值，表明下一行是否可用，返回True则可用，返回False则到达结果集末尾。</p>
<p>使用DataReader可以提高执行效率，有两种方式可以提高代码的性能：</p>
<p>一种是基于序号的查找</p>
<p>一个是使用适当的Get方法来查找。因为查询出来的结果一般都不会改变，除非再次改动查询语句，因此可以通过定位列的位置来查找记录。用这种方法有一个问题，就是可能知道一列的名称而不知道其所在的位置，这个问题的解决方案是通过调用DataReader 对象的GetOrdinal()方法，此方法接收一个列名并返回此列名所在的列号。</p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576328696247.png" alt="1576328696247"></p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> System.ComponentModel;</span><br><span class="line"><span class="keyword">using</span> System.Data;</span><br><span class="line"><span class="keyword">using</span> System.Drawing;</span><br><span class="line"><span class="keyword">using</span> System.Linq;</span><br><span class="line"><span class="keyword">using</span> System.Text;</span><br><span class="line"><span class="keyword">using</span> System.Threading.Tasks;</span><br><span class="line"><span class="keyword">using</span> System.Windows.Forms;</span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> System.Data.SqlClient; <span class="comment">//一定要写</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">test</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">partial</span> <span class="keyword">class</span> <span class="title">Form1</span> : <span class="title">Form</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Form1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            InitializeComponent();</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">		<span class="comment">//button连接数据库</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button1_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">        	<span class="comment">//创建连接</span></span><br><span class="line">            SqlConnection conn = <span class="keyword">new</span> SqlConnection(<span class="string">"Data Source=DESKTOP-IUSUDN5\\SQLEXPRESS;Initial Catalog=test1;User Id=u1;Password=123;"</span>);</span><br><span class="line">            <span class="comment">//打开连接</span></span><br><span class="line">            conn.Open();</span><br><span class="line">            MessageBox.Show(<span class="string">"连接成功,连接状态："</span> +conn.State);</span><br><span class="line">            <span class="comment">//关闭连接</span></span><br><span class="line">            conn.Close();</span><br><span class="line">            MessageBox.Show(<span class="string">"关闭连接,连接状态："</span> + conn.State);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//button增加</span></span><br><span class="line">         <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button2_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">			 <span class="keyword">using</span> (SqlConnection conn = <span class="keyword">new</span> SqlConnection(<span class="string">"server=DESKTOP-		  IUSUDN5\\SQLEXPRESS;uid=u1;pwd=123;database=test1"</span>))</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">                String sql = <span class="string">"insert into test values(@a,@b)"</span>;</span><br><span class="line">                SqlCommand cmd = <span class="keyword">new</span> SqlCommand(sql, conn);</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@a"</span>,text_a.Text));</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@b"</span>, text_b.Text));</span><br><span class="line">                <span class="keyword">int</span> rows = cmd.ExecuteNonQuery();</span><br><span class="line">                <span class="keyword">if</span> (rows &gt; <span class="number">0</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    MessageBox.Show(<span class="string">"增加成功."</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//button删除</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button3_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">			<span class="keyword">using</span> (SqlConnection conn = <span class="keyword">new</span> SqlConnection(<span class="string">"server=DESKTOP-IUSUDN5\\SQLEXPRESS;uid=u1;pwd=123;database=test1"</span>))</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">                String sql = <span class="string">"delete from test where a = @a"</span>;</span><br><span class="line">                SqlCommand cmd = <span class="keyword">new</span> SqlCommand(sql, conn);</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@a"</span>, text_delete_a.Text));</span><br><span class="line">                <span class="keyword">int</span> rows = cmd.ExecuteNonQuery();</span><br><span class="line">                MessageBox.Show(<span class="string">"删除了"</span> + rows + <span class="string">"行"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//button修改</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button4_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">			<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button4_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">            <span class="keyword">using</span> (SqlConnection conn = <span class="keyword">new</span> SqlConnection(<span class="string">"server=DESKTOP-IUSUDN5\\SQLEXPRESS;uid=u1;pwd=123;database=test1"</span>))</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">                String sql = <span class="string">"update test set a = @a_inserted where a = @a_deleted"</span>;</span><br><span class="line">                SqlCommand cmd = <span class="keyword">new</span> SqlCommand(sql, conn);</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@a_inserted"</span>,text_inserted_a.Text));</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@a_deleted"</span>, text_deleted_a.Text));</span><br><span class="line">                <span class="keyword">int</span> rows = cmd.ExecuteNonQuery();</span><br><span class="line">                MessageBox.Show(<span class="string">"更新了"</span>+rows+<span class="string">"行"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//button查询</span></span><br><span class="line">         <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">button5_Click</span>(<span class="params"><span class="keyword">object</span> sender, EventArgs e</span>)</span></span><br><span class="line"><span class="function"></span>        &#123;</span><br><span class="line">			<span class="keyword">using</span> (SqlConnection conn = <span class="keyword">new</span> SqlConnection(<span class="string">"server=DESKTOP-IUSUDN5\\SQLEXPRESS;uid=u1;pwd=123;database=test1"</span>))</span><br><span class="line">            &#123;</span><br><span class="line">                conn.Open();</span><br><span class="line">                String sql = <span class="string">"select * from test where a = @a"</span>;</span><br><span class="line">                SqlCommand cmd = <span class="keyword">new</span> SqlCommand(sql,conn);</span><br><span class="line">                cmd.Parameters.Add(<span class="keyword">new</span> SqlParameter(<span class="string">"@a"</span>,text_select_a.Text));</span><br><span class="line">                SqlDataReader sdr = cmd.ExecuteReader();</span><br><span class="line">                <span class="keyword">while</span> (sdr.Read())</span><br><span class="line">                &#123;</span><br><span class="line">                    MessageBox.Show(<span class="string">"a: "</span> + sdr[<span class="string">"a"</span>] + <span class="string">",b: "</span> + sdr[<span class="string">"b"</span>]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="增加功能"><a href="#增加功能" class="headerlink" title="增加功能"></a>增加功能</h2><p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576329047524.png" alt="1576329047524"></p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576329227325.png" alt="1576329227325"></p>
<h2 id="删除功能"><a href="#删除功能" class="headerlink" title="删除功能"></a>删除功能</h2><p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576329701221.png" alt="1576329701221"></p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576329724257.png" alt="1576329724257"></p>
<h2 id="修改功能"><a href="#修改功能" class="headerlink" title="修改功能"></a>修改功能</h2><p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576330245223.png" alt="1576330245223"></p>
<p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576330276141.png" alt="1576330276141"></p>
<h2 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h2><p><img src="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/1576330879544.png" alt="1576330879544"></p>
<p>​    </p>

    </div>

    
    
    

      
      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
      </div>

      <footer class="post-footer">



        
<div>    
 
 
<ul class="post-copyright">
  <li class="post-copyright-author">
      <strong>本文作者：</strong>付文杰
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="/2019/12/14/SQLServer%E8%AF%BE%E8%AE%BE1/" title="Sql server课设1">2019/12/14/SQLServer课设1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权： </strong>
    本站文章均采用 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0 CN</a> 许可协议，请勿用于商业，转载注明出处！
  </li>
</ul>

</div>
          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"><i class="fa fa-tag"></i> 学习笔记</a>
              <a href="/tags/SQL-server/" rel="tag"><i class="fa fa-tag"></i> SQL server</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2019/12/02/%E6%8E%92%E5%BA%8F2/" rel="next" title="算法竞赛准备3----排序2（Java语言）">
                  <i class="fa fa-chevron-left"></i> 算法竞赛准备3----排序2（Java语言）
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2019/12/14/%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%BD%91%E7%BB%9C/" rel="prev" title="CentOs7网络配置">
                  CentOs7网络配置 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=27588222&auto=1&height=66"></iframe>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Connection连接对象"><span class="nav-number">1.</span> <span class="nav-text">Connection连接对象</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#连接"><span class="nav-number">1.1.</span> <span class="nav-text">连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Sql-server连接字符串"><span class="nav-number">1.1.1.</span> <span class="nav-text">Sql server连接字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#准备工作"><span class="nav-number">1.1.2.</span> <span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在VS中连接数据库"><span class="nav-number">1.1.3.</span> <span class="nav-text">在VS中连接数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接到数据库"><span class="nav-number">1.1.4.</span> <span class="nav-text">连接到数据库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#VS实例"><span class="nav-number">1.2.</span> <span class="nav-text">VS实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#增加功能"><span class="nav-number">1.3.</span> <span class="nav-text">增加功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除功能"><span class="nav-number">1.4.</span> <span class="nav-text">删除功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改功能"><span class="nav-number">1.5.</span> <span class="nav-text">修改功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查询"><span class="nav-number">1.6.</span> <span class="nav-text">查询</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="付文杰"
    src="/images/headImage.jpg">
  <p class="site-author-name" itemprop="name">付文杰</p>
  <div class="site-description" itemprop="description">兰州交通大学 | 本科生 <br> 信息管理与信息系统</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/FWj1635387072" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;FWj1635387072" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:1635387072@qq.com" title="E-Mail &amp;rarr; mailto:1635387072@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/null" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>





      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        


<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">付文杰</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">210k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:50</span>

  <br>
  <span id="sitetime"></span>

</div>
<script language=javascript>
    function siteTime() {
        window.setTimeout("siteTime()", 1000);
        var seconds = 1000;
        var minutes = seconds * 60;
        var hours = minutes * 60;
        var days = hours * 24;
        var years = days * 365;
        var today = new Date();
        var todayYear = today.getFullYear();
        var todayMonth = today.getMonth() + 1;
        var todayDate = today.getDate();
        var todayHour = today.getHours();
        var todayMinute = today.getMinutes();
        var todaySecond = today.getSeconds();
        /* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
        year - 作为date对象的年份，为4位年份值
        month - 0-11之间的整数，做为date对象的月份
        day - 1-31之间的整数，做为date对象的天数
        hours - 0(午夜24点)-23之间的整数，做为date对象的小时数
        minutes - 0-59之间的整数，做为date对象的分钟数
        seconds - 0-59之间的整数，做为date对象的秒数
        microseconds - 0-999之间的整数，做为date对象的毫秒数 */
        var t1 = Date.UTC(2019, 11, 7, 17, 50, 00); //北京时间2018-2-13 00:00:00
        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
        var diff = t2 - t1;
        var diffYears = Math.floor(diff / years);
        var diffDays = Math.floor((diff / days) - diffYears * 365);
        var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
        var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes);
        var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes * minutes) / seconds);
        document.getElementById("sitetime").innerHTML = "本站已运行 " +diffYears+" 年 "+diffDays + " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
    }/*因为建站时间还没有一年，就将之注释掉了。需要的可以取消*/
    siteTime();
</script>


        












        
      </div>
    </footer>
  </div>

  
  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  
















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"symbols_count_time":null,"symbols":true,"time":true,"total_symbols":true,"total_time":true,"log":false});</script></body>
</html>
<script type="text/javascript" src="/js/src/clicklove.js"></script>